package com.weigee.ktv.v.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.l.d.app.library.util.ListUtil;
import com.l.d.app.library.util.ResourceUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.weigee.ktv.R;
import com.weigee.ktv.m.entity.SystemInformsEntity;
import com.weigee.ktv.p.TaskDetailContract;
import com.weigee.ktv.p.frag.SystemInformsPresenterImpl;
import com.weigee.ktv.v.adapter.rv.SystemInformsAdapter;
import com.weigee.ktv.v.view.WrapTitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 林德 on 2017/4/16 13:17.
 * <p/>
 */
public class SystemInformsFragment extends BaseFragment2 implements TaskDetailContract.SystemInformsView
{
    @BindView(R.id.wrap_title_bar)
    WrapTitleBar wrapTitleBar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.t_refresh)
    TwinklingRefreshLayout tRefresh;
    Unbinder unbinder;
    //    @BindView(R.id.wrap_title_bar)
    //    WrapTitleBar mTitleBar;
    //    @BindView(R.id.t_refresh)
    //    TwinklingRefreshLayout m_tRefresh;
    //    @BindView(R.id.recycler_view)
    //    RecyclerView mRv;

    private SystemInformsAdapter adapter;
    private TaskDetailContract.SystemInformsPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.recycler_wrap, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        final Context context = view.getContext();

        wrapTitleBar.setTitle(ResourceUtil.string(context, R.string.title_system_informs));
        wrapTitleBar.setLeftButton(View.VISIBLE, new SimpleFragBackListener(this));


        ListUtil.initTwinklingRefresh(tRefresh, new RefreshListenerAdapter()
        {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout)
            {
                super.onRefresh(refreshLayout);
                getPresenter().onLoadListData(false);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout)
            {
                super.onLoadMore(refreshLayout);
                getPresenter().onLoadListData(true);
            }
        });
        adapter = new SystemInformsAdapter();
        ListUtil.initRecyclerView(recyclerView, null, adapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        getPresenter().onActivityCreated(savedInstanceState);
    }

    @NonNull
    @Override
    public Context getMyContext()
    {
        final FragmentActivity result = getActivity();
        return result == null ? getContext() : result;
    }

    @NonNull
    @Override
    public TaskDetailContract.SystemInformsPresenter getPresenter()
    {
        if (presenter == null)
        {
            presenter = new SystemInformsPresenterImpl(this);
        }
        return presenter;
    }

    @Override
    public void onResponseListData(List<SystemInformsEntity.DataBean> been, boolean moreData, boolean enableNextMore)
    {
        if (tRefresh != null)
        {
            if (moreData)
            {
                tRefresh.finishLoadmore();
            }
            else
            {
                tRefresh.finishRefreshing();
            }
            tRefresh.setEnableLoadmore(enableNextMore);
        }
        if (adapter == null)
        {
            adapter = new SystemInformsAdapter();
            ListUtil.initRecyclerView(recyclerView, null, adapter);
        }
        if (!moreData && adapter.getItemCount() > 0)
        {  
            adapter.clear();
        }
        adapter.add(been);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        unbinder.unbind();
    }
}
